/*
 * calibration_callback.c
 *
 *  Created on: 2022年02月10日
 *      Author: Moon
 */


/* Includes ------------------------------------------------------------------*/
#include "ParmManage.h"
#include "cmd_process.h"
#include "pfc_task.h"
#include "system.h"
#include "valve_ctl_task.h"
#include "dev_adg1404.h"
#include "func_pt100.h"
#include "pressuresensor.h"
#include "main_task.h"
#include "dev_valve.h"

/* Private defines -----------------------------------------------------------*/

/* Public enum    -----------------------------------------------------------*/

/* Public Variable  ---------------------------------------------------------*/
extern dev_dac8830_t dac8830;
uint8_t provalve_single_control_flag = 0;
/* Public Struct  -----------------------------------------------------------*/


/* Private Variable  ---------------------------------------------------------*/


//Array

//Const

/* Private function prototypes -----------------------------------------------*/


/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *pres_pumpSwitch_callback(user_packet_t *user)
{
	ack_packet_t *ack;

	if (user->style == CMD_SET){
		// 用户数据处理
		data_server.pump_st_set(&data_server, user->data[0]);

		// 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	
	}else if (user->style == CMD_READ){
		// 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = data_server.pump_st_get(&data_server);
	}
	return ack;
}

/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
uint16_t valve_st = 0;

ack_packet_t *pres_valveSwitch_callback(user_packet_t *user)
{

	ack_packet_t *ack;
	
	uint16_t value = 0;
	
	value = (uint16_t)((uint16_t)user->data[0]<<8) | user->data[1];
	
	valve_st = value;

	if (user->style == CMD_SET){
		
		data_server.valve_set(&data_server, value);

		// 用户数据处理
		// 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	
	}else if (user->style == CMD_READ) //读取
	{
		// 应答返回数据
		ack = Ack_Malloc(2);
		ack->data[0] = data_server.valve_get(&data_server)>>8;
		ack->data[1] = data_server.valve_get(&data_server);
	}
    
    Ashell_print("switch value:%x\r\n",value);
	return ack;
}

/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *pres_setpres1_callback(user_packet_t *user)
{
	ack_packet_t *ack;
	
	if (user->style == CMD_SET) //设置
	{
		// 用户数据处理

		// 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	}
	else if (user->style == CMD_READ) //读取
	{
		// 应答返回数据
		ack = Ack_Malloc(4);
		//PPItemRead(PP_S1_PRE, &ack->data[0], 4);
	}
	
	return ack;
}

/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *pres_setpres2_callback(user_packet_t *user)
{
	ack_packet_t *ack;

	if (user->style == CMD_SET) //设置
	{
		// 用户数据处理
		
		// 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	
	}
	else if (user->style == CMD_READ) //读取
	{
		// 应答返回数据
		ack = Ack_Malloc(4);
		//PPItemRead(PP_S2_PRE, &ack->data[0], 4);
	}

	return ack;
}

/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *pres_setpres3_callback(user_packet_t *user)
{
	ack_packet_t *ack;

	if (user->style == CMD_SET) //设置
	{
		// 用户数据处理
		
		// 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	
	}
	else if (user->style == CMD_READ) //读取
	{
		// 应答返回数据
		ack = Ack_Malloc(4);
		//PPItemRead(PP_S3_PRE, &ack->data[0], 4);
	}

	return ack;
}

/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *pres_setpres4_callback(user_packet_t *user)
{
	ack_packet_t *ack;

	if (user->style == CMD_SET) //设置
	{
		// 用户数据处理
		
		// 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	
	}
	else if (user->style == CMD_READ) //读取
	{
		// 应答返回数据
		ack = Ack_Malloc(4);
		//PPItemRead(PP_CELOM_PRE, &ack->data[0], 4);
	}

	return ack;
}

/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *pres_setpres5_callback(user_packet_t *user)
{
	ack_packet_t *ack;

	if (user->style == CMD_SET) //设置
	{
		// 用户数据处理
		
		// 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	
	}
	else if (user->style == CMD_READ) //读取
	{
		// 应答返回数据
		ack = Ack_Malloc(4);
		//PPItemRead(PP_CELOM_PRE, &ack->data[0], 4);
	}

	return ack;
}

/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *pres_readpres1_callback(user_packet_t *user)
{
	ack_packet_t *ack;

	// 应答返回数据
	ack = Ack_Malloc(4);
	//memcpy(&ack->data[0], &pS1.P_sensor1, sizeof(float));

	return ack;
}

/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *pres_readpres2_callback(user_packet_t *user)
{
	ack_packet_t *ack;

	// 应答返回数据
	ack = Ack_Malloc(4);
	//memcpy(&ack->data[0], &pS2.P_sensor1, sizeof(float));

	return ack;
}
/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *pres_readpres3_callback(user_packet_t *user)
{
	ack_packet_t *ack;

	// 应答返回数据
	ack = Ack_Malloc(4);
	//memcpy(&ack->data[0], &pS3.P_sensor1, sizeof(float));

	return ack;
}
/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *pres_readpres4_callback(user_packet_t *user)
{
	ack_packet_t *ack;

	// 应答返回数据
	ack = Ack_Malloc(4);
	//memcpy(&ack->data[0], &pBody.P_sensor1, sizeof(float));

	return ack;
}
/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *pres_readpres5_callback(user_packet_t *user)
{
	ack_packet_t *ack;

	// 应答返回数据
	ack = Ack_Malloc(4);
	//memcpy(&ack->data[0], &pBody.P_sensor1, sizeof(float));

	return ack;
}

/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *pres_state_callback(user_packet_t *user)
{
	ack_packet_t *ack;
    
	// 应答返回数据
	ack = Ack_Malloc(1);
	//ack->data[0] = g_presState;  
	
	return ack;
}

/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *pres_setprovalve_callback(user_packet_t *user)
{
	ack_packet_t *ack;
	float pro = 0;
	
	if (user->style == CMD_SET) //设置
	{
		// 用户数据处理
		memcpy(&pro, &user->data[0], 4);
		data_server.provalve_set(&data_server, pro);
		
		// 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	
	}
	else if (user->style == CMD_READ) //读取
	{
		// 应答返回数据
		ack = Ack_Malloc(4);
		
		pro = data_server.provalve_get(&data_server);
		memcpy(&ack->data[0],&pro, 4);
	}
	
	return ack;
}

/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *pres_tolerance1_callback(user_packet_t *user)
{
	ack_packet_t *ack;
	float tolerance = 0;
	
	if (user->style == CMD_SET) //设置
	{
		// 用户数据处理
		memcpy(&tolerance, &user->data[0], 4);
		data_server.presensor_tolerance_set(&data_server, 0, tolerance);
		
		// 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	
	}
	else if (user->style == CMD_READ) //读取
	{
		// 应答返回数据
		ack = Ack_Malloc(4);
		data_server.presensor_tolerance_get(&data_server, 0, &tolerance);
		memcpy(&ack->data[0],&tolerance, 4);
	}
	
	return ack;
}

/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *pres_tolerance2_callback(user_packet_t *user)
{
	ack_packet_t *ack;
	float tolerance = 0;
	
	if (user->style == CMD_SET) //设置
	{
		// 用户数据处理
		memcpy(&tolerance, &user->data[0], 4);
		data_server.presensor_tolerance_set(&data_server, 1, tolerance);
		
		// 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	
	}
	else if (user->style == CMD_READ) //读取
	{
		// 应答返回数据
		ack = Ack_Malloc(4);
		data_server.presensor_tolerance_get(&data_server, 1, &tolerance);
		memcpy(&ack->data[0],&tolerance, 4);
	}
	
	return ack;
}
/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *pres_tolerance3_callback(user_packet_t *user)
{
	ack_packet_t *ack;
	float tolerance = 0;
	
	if (user->style == CMD_SET) //设置
	{
		// 用户数据处理
		memcpy(&tolerance, &user->data[0], 4);
		data_server.presensor_tolerance_set(&data_server, 2, tolerance);
		
		// 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	
	}
	else if (user->style == CMD_READ) //读取
	{
		// 应答返回数据
		ack = Ack_Malloc(4);
		data_server.presensor_tolerance_get(&data_server, 2, &tolerance);
		memcpy(&ack->data[0],&tolerance, 4);
	}
	
	return ack;
}
/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *pres_tolerance4_callback(user_packet_t *user)
{
	ack_packet_t *ack;
	float tolerance = 0;
	
	if (user->style == CMD_SET) //设置
	{
		// 用户数据处理
		memcpy(&tolerance, &user->data[0], 4);
		data_server.presensor_tolerance_set(&data_server, 3, tolerance);
		
		// 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	
	}
	else if (user->style == CMD_READ) //读取
	{
		// 应答返回数据
		ack = Ack_Malloc(4);
		data_server.presensor_tolerance_get(&data_server, 3, &tolerance);
		memcpy(&ack->data[0],&tolerance, 4);
	}
	
	return ack;
}
/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *pres_tolerance5_callback(user_packet_t *user)
{
	ack_packet_t *ack;
	float tolerance = 0;
	
	if (user->style == CMD_SET) //设置
	{
		// 用户数据处理
		memcpy(&tolerance, &user->data[0], 4);
		data_server.presensor_tolerance_set(&data_server, 4, tolerance);
		
		// 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	
	}
	else if (user->style == CMD_READ) //读取
	{
		// 应答返回数据
		ack = Ack_Malloc(4);
		data_server.presensor_tolerance_get(&data_server, 4, &tolerance);
		memcpy(&ack->data[0],&tolerance, 4);
	}
	
	return ack;
}

uint8_t adc_filter_value = 0;
/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *pres_pidcycle_callback(user_packet_t *user)
{

	ack_packet_t *ack;
	
	if (user->style == CMD_SET) //设置
	{
		// 用户数据处理
		//memcpy(&BODYPRESCTRL.sampleCycle, &user->data[0], 2);
		memcpy(&adc_filter_value, &user->data[0], 2);
		//PPItemIWrite(PP_VALVE_PID_PERIOD, &user->data[0], 2);
		
		// 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	
	}
	else if (user->style == CMD_READ) //读取
	{
		// 应答返回数据
		ack = Ack_Malloc(4);
		ack->data[0] = adc_filter_value;
		//PPItemRead(PP_VALVE_PID_PERIOD, &ack->data[0], 2);
	}
	
	return ack;

}

/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *pres_pid_callback(user_packet_t *user)
{
	ack_packet_t *ack;
	
	float pres_pid[3];
	float speed_pid[3];
	
	if (user->style == CMD_SET) //设置
	{
		// 用户数据处理
		memcpy(&pres_pid[0], &user->data[0], 4);
		memcpy(&pres_pid[1], &user->data[4], 4);
		memcpy(&pres_pid[2], &user->data[8], 4);
		
		memcpy(&speed_pid[0], &user->data[12], 4);
		memcpy(&speed_pid[1], &user->data[16], 4);
		memcpy(&speed_pid[2], &user->data[20], 4);
		
//		PPItemIWrite(PP_VALVE_PID_P, &user->data[0], 4);
//		PPItemIWrite(PP_VALVE_PID_I, &user->data[4], 4);
//		PPItemIWrite(PP_VALVE_PID_D, &user->data[8], 4);
		
		data_server.pid_par_set(&data_server, pres_pid, speed_pid);

		// 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	
	}
	else if (user->style == CMD_READ) //读取
	{
		ack = Ack_Malloc(36);
		
		//data_server.pid_par_get(&data_server, pres_pid, speed_pid);
        PPItemRead(PP_DAC_CALI_K1, (uint8_t*)&pres_pid[0], sizeof(float));
        PPItemRead(PP_DAC_CALI_B1, (uint8_t*)&pres_pid[1], sizeof(float));
        
        PPItemRead(PP_DAC_CALI_K_HE + data_server.gas_type , (uint8_t*)&pres_pid[2], sizeof(float));
        PPItemRead(PP_DAC_CALI_B_HE + data_server.gas_type , (uint8_t*)&speed_pid[0], sizeof(float));
        
        PPItemRead(PP_DAC_INF_MIN_STEP_HE + data_server.gas_type , (uint8_t*)&speed_pid[1], sizeof(float));
        PPItemRead(PP_DAC_PUM_MIN_STEP_HE + data_server.gas_type , (uint8_t*)&speed_pid[2], sizeof(float));
        
		memcpy(&ack->data[0], &pres_pid[0], sizeof(float)); 
		memcpy(&ack->data[4], &pres_pid[1], sizeof(float));
		memcpy(&ack->data[8], &pres_pid[2], sizeof(float));
		
		memcpy(&ack->data[12], &speed_pid[0], sizeof(float)); 
		memcpy(&ack->data[16], &speed_pid[1], sizeof(float));
		memcpy(&ack->data[20], &speed_pid[2], sizeof(float));
		
//		PPItemRead(PP_VALVE_PID_P, &ack->data[0], 4);
//		PPItemRead(PP_VALVE_PID_I, &ack->data[4], 4);
//		PPItemRead(PP_VALVE_PID_D, &ack->data[8], 4);
	}
	
	return ack;
}

/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *pres_temp1_callback(user_packet_t *user)
{
	float temp = 0;
	ack_packet_t *ack;
    
	// 应答返回数据
	ack = Ack_Malloc(4);
	
	temp = data_server.chamber_temp_get(&data_server);
	memcpy(&ack->data[0], &temp, sizeof(float));  
	
	return ack;
}

/***********************************************************************************
 * @brief
 * ex:     reserved.
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *pres_temp2_callback(user_packet_t *user)
{
	float res = 0;
	ack_packet_t *ack;
    
	// 应答返回数据
	ack = Ack_Malloc(4);
	
	memcpy(&ack->data[0], &res, sizeof(float));  
	
	return ack;
}

/***********************************************************************************
 * @brief
 * ex:     reserved.
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *pres_temp3_callback(user_packet_t *user)
{
	float res = 0;
	ack_packet_t *ack;
    
	// 应答返回数据
	ack = Ack_Malloc(4);

	memcpy(&ack->data[0], &res, sizeof(float));  
	
	return ack;
}

/***********************************************************************************
 * @brief
 * ex:     
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *pres_gas_callback(user_packet_t *user)
{
	ack_packet_t *ack;
    uint8_t gas_type = 0;
	
	if (user->style == CMD_SET) //设置
	{
		// 用户数据处理
        gas_type = user->data[0];
        
        Ashell_print("set gas type:%d\r\n",gas_type);
		data_server.gas_type_set(&data_server, gas_type);
		
		// 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	
	}
	else if (user->style == CMD_READ) //读取
	{
		// 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = data_server.gas_type_get(&data_server);
	}
	
	return ack;
}

/***********************************************************************************
 * @brief
 * ex:     
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *valve_pulse_ctrl_callback(user_packet_t *user)
{
	ack_packet_t *ack;
	
	uint8_t valve_index = 0;
	uint16_t valve_st1 = 0;
	uint16_t valve_st2 = 0;
	
	valve_index = user->data[0];
	valve_st1 = (1 << valve_index) | (data_server.valve.cmd);
	valve_st2 = (valve_st1) & (~(1 << valve_index));	
	
	if (user->style == CMD_SET)
	{
		data_server.valve_set(&data_server, valve_st1);
		SoftDelayMs(500);
		data_server.valve_set(&data_server, valve_st2);
		
		// 用户数据处理
		// 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	
	}
	return ack;
}

/***********************************************************************************
 * @brief
 * ex:     
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *provalve_ctrl_callback(user_packet_t *user)
{
	ack_packet_t *ack;
	uint8_t direction = 0; //1代表充气，2代表抽气 
    
    direction = user->data[0];

    if (user->style == CMD_SET) //设置
	{
		// 用户数据处理
        if(direction)
        {
            data_server.press_par.act_final = ACTION_PUM;
        }
        else
        {
            data_server.press_par.act_final = ACTION_INF;
        }
        memcpy(&data_server.press_par.rate_real, &user->data[1], 4);
        memcpy(&data_server.press_par.target_press_final, &user->data[5], 4);

        taskENTER_CRITICAL();
		provalve_single_control_flag = 1;
		msm = MSM_RUN;
		rsm = RSM_ENTRY;
        taskEXIT_CRITICAL();
		
        Ashell_print("begin single to control,rate:%d,target:%d mmHg\r\n",data_server.press_par.rate_real, \
                        data_server.press_par.target_press_final/133);
        
		// 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	}
	
	return ack;  

}

ack_packet_t *provalve_close_callback(user_packet_t *user)
{
	ack_packet_t *ack;


    if (user->style == CMD_SET) //设置
	{
        provalve_single_control_flag = 0;
		dac8830.Write(&dac8830,0,0);

        Ashell_print("exit provalve single control\r\n");
		data_server.valve_localset(&data_server, (uint8_t)VIRTUAL_VALVE_INF, 0);
		data_server.valve_localset(&data_server, (uint8_t)VIRTUAL_VALVE_PUMP, 0);
		
		// 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	}
	
	return ack;  


}
/***********************************************************************************
 * @brief
 * ex:     
 * @par    None
 * @retval None
 **********************************************************************************/
extern SensorCalibration calibrations[];
ack_packet_t *cal_press_b_ctrl_callback(user_packet_t *user)
{
	ack_packet_t *ack;
	uint8_t index = 0;
	float b = 0;
	
	if (user->style == CMD_SET) //设置
	{	
		index = user->data[0];
		
		for(int i = 0;i < 6; i++)
		{
			if((index & (1<<i)) && i != 5)
			{
				b = calibrations[i].intercept - data_server.press_par.current_press[i];
				calibrations[i].intercept = b;
				PPItemIWrite(PP_PRESENSOR1_INTERCEPT_CDT + i * 2, (uint8_t*)&b, sizeof(float));
			}
			else if((index & (1<<i)) && i == 5)
			{
				b = calibrations[i].intercept - data_server.press_par.current_press[i];
				calibrations[i].intercept = b;
				PPItemIWrite(PP_PRESENSOR6_INTERCEPT_CDT, (uint8_t*)&b, sizeof(float));
				
			}
				
		}
		
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	
	}
	return ack;
}


/***********************************************************************************
 * @brief
 * ex:     
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *cal_press_match_b_ctrl_callback(user_packet_t *user)
{
	ack_packet_t *ack;
	uint8_t index = 0;
	
	if (user->style == CMD_SET) //设置
	{	
	
		index = user->data[0];
		for(int i = 1;i < 5; i++)
		{
			if((index & (1<<i)))
			{
				float b = 0;
				b = calibrations[i].intercept - data_server.press_par.current_press[i];
				calibrations[i].intercept = b;
				PPItemIWrite(PP_PRESENSOR1_INTERCEPT_CDT + i*2, (uint8_t*)&b, sizeof(float));
			}
				
		}
		
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	}
	return ack;
}

/***********************************************************************************
 * @brief
 * ex:     
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *cal_press_k_ctrl_callback(user_packet_t *user)
{
	ack_packet_t *ack;
	uint8_t index = 0;
	float atom_press;
	float adc_codeValue = 0;
	float k = 0;
	
	if (user->style == CMD_SET) //设置
	{	
	
		index = user->data[0];
		memcpy(&atom_press, &user->data[2], sizeof(float)); 
		
		for(int i = 0;i < 6; i++)
		{
			if((index & (1<<i)) && i != 5)
			{
				adc_codeValue = (data_server.press_par.current_press[i] - calibrations[i].intercept) / calibrations[i].slope;
				k = (atom_press - calibrations[i].intercept) / adc_codeValue;
				calibrations[i].slope = k;
				PPItemIWrite(PP_PRESENSOR1_SLOPE_CDT + i*2, (uint8_t*)&k, sizeof(float));
			}
			else if((index & (1<<i)) && i == 5)
			{
				adc_codeValue = (data_server.press_par.current_press[i] - calibrations[i].intercept) / calibrations[i].slope;
				k = (atom_press - calibrations[i].intercept);
				calibrations[i].slope = k;
				PPItemIWrite(PP_PRESENSOR6_SLOPE_CDT, (uint8_t*)&k, sizeof(float));
				
			}	
		}
		
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	}
	return ack;
}
/***********************************************************************************
 * @brief
 * ex:     
 * @par    None
 * @retval None
 **********************************************************************************/
double a,b,c;
uint8_t temp_complete_flag = 0;
float realResValue[3];
float testResValue[3];
extern float testRes;
ack_packet_t *record_res_value_ctrl_callback(user_packet_t *user)
{
//	ack_packet_t *ack;
//	uint8_t index;

//	float denominator = 2;
//	
//	if (user->style == CMD_SET) //设置
//	{	
//		index = user->data[0];
//		
//		
//		memcpy(&realResValue[index - 1], &user->data[1], 4);
//		memcpy(&testResValue[index - 1], &testRes, 4);
//		
//		realResValue[index - 1] *= 1000; 
//		Ashell_print("input %d res is %f,test %d res is %f \r\n",index, realResValue[index - 1],index,testResValue[index - 1]);
//	
//		if(index == 3)
//		{
//			
//			denominator = (testResValue[0] - testResValue[1]) * (testResValue[0] - testResValue[2]) * (testResValue[1]-testResValue[2]);
//			a = (double) ((testResValue[0] * (realResValue[2] - realResValue[1]) + testResValue[1] * (realResValue[0] - realResValue[2]) + \
//				testResValue[2] * (realResValue[1] - realResValue[0]) )/ denominator);
//			b = (double) ( pow((testResValue[0]),2) * (realResValue[1] - realResValue[2]) + pow((testResValue[1]),2) * (realResValue[2] - realResValue[0]) + \
//				pow((testResValue[2]),2) * (realResValue[0] - realResValue[1]) )/ denominator;
//			c = (double) ((realResValue[0]) * testResValue[1] * testResValue[2] * (testResValue[1] - testResValue[2]) + \
//				(testResValue[0]) * testResValue[2] * realResValue[1] * (testResValue[2] - testResValue[0]) + \
//				(testResValue[0] * testResValue[1] * realResValue[2]) * (testResValue[0] - testResValue[1]) )/ denominator;
//			temp_complete_flag = 1;
//			
//			PPItemIWrite(PP_TEMP_COE_A, (uint8_t*)&a, sizeof(double));
//			PPItemIWrite(PP_TEMP_COE_B, (uint8_t*)&b, sizeof(double));
//			PPItemIWrite(PP_TEMP_COE_C, (uint8_t*)&c, sizeof(double));
//		}
//		
//		ack = Ack_Malloc(1);
//		ack->data[0] = 0x00;
//	}
//	return ack;
    return 0;
}

/***********************************************************************************
 * @brief
 * ex:     
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *record_base_cbm_ctrl_callback(user_packet_t *user)
{
	ack_packet_t *ack;
	float base_cbm = 0;
	
	if (user->style == CMD_SET) //设置
	{
		// 用户数据处理
		memcpy( &base_cbm, &user->data[0],sizeof(float)); 
		PPItemIWrite(PP_BASE_CBM, (uint8_t*)&base_cbm, sizeof(float));
		data_server.base_volume = base_cbm;
        data_server.isBaseVolChanged = true;
		// 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	
	}
	else if (user->style == CMD_READ) //读取
	{
		ack = Ack_Malloc(4);
		PPItemRead(PP_BASE_CBM, (uint8_t*)&ack->data[0], sizeof(float));
		// 应答返回数据		
		
	}
	
	return ack;
}

/***********************************************************************************
 * @brief
 * ex:     
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *pres_valveSwitch_New_callback(user_packet_t *user)
{
	ack_packet_t *ack;
	
	uint8_t valve_index = 0;
	uint8_t valve_enable = 0; //1表示开，0表示关

	
	valve_index = user->data[0];
	valve_enable = user->data[1];

	dev_valve_t* dev_valve = DevValve_GetInstance();
	
	if (user->style == CMD_SET)
	{
		if(valve_enable)
		{
			dev_valve->set(dev_valve, valve_index);
			data_server.valve.cmd |= (1 << valve_index);
		}
		else
		{
			dev_valve->reset(dev_valve, valve_index);
			data_server.valve.cmd &= ~(1 << valve_index);
		}
		
		Ashell_print("valve_index:%x,valve_enable:%x\r\n",valve_index,valve_enable);
		// 用户数据处理
		// 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	
	}
	return ack;
}
/***********************************************************************************
 * @brief
 * ex:     
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *pres_ctrl_callback(user_packet_t *user)
{
	ack_packet_t *ack;
	
	float rate = 0, target = 0;
    uint8_t pos = 0;
	
	if (user->style == CMD_SET) //设置
	{
		// 用户数据处理
		memcpy(&pos,&user->data[0],1);
        memcpy(&rate, &user->data[1], 4);
		memcpy(&target, &user->data[5], 4);
		
        provalve_single_control_flag = 0;
        
        Ashell_print("begin conbine to control,pos:%x,rate:%f,target:%f mmHg\r\n",pos,rate,target/133.322);
        
		data_server.press_ctrl(&data_server, pos, rate, target);  
		// 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	}

	return ack;
}

/***********************************************************************************
 * @brief
 * ex:     
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *ctrl_start_callback(user_packet_t *user)
{
	ack_packet_t *ack;
    uint8_t startFlag = 0;
    uint8_t startPos = 0;
	
    startFlag = user->data[0];
	startPos = user->data[1];
    if (startFlag == 0x02) //停止，抽基体腔
	{    
		test_state = FINISHED;
		PPItemIWrite(PP_DEV_RUNSTATE,(uint8_t *)&test_state,1);
        PPItemIWrite(PP_VALVE_POSINDEX, &startPos, 1); 
        
        //异常停止实验
        if(data_server.exp_stop_flag)
        {
            data_server.valve_localset(&data_server, (uint8_t)VALVE_PRO_PUMP, 1);
            data_server.valve_localset(&data_server, (uint8_t)VALVE_QUICK_PUMP, 1);
            data_server.exp_stop_flag = false;
            Ashell_print("stop experiment unnormorly!\r\n");
        } 
        else
        {
            Ashell_print("stop experiment normorly!\r\n");
        }

		// 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	}
    else
    {
        test_state = RUNING;
        poweroff_state = POWER_OFF_ON_FINISHED;
        data_server.press_par.start_pos = startPos;
        PPItemIWrite(PP_DEV_RUNSTATE, &test_state,1);
        PPItemIWrite(PP_VALVE_POSINDEX, &startPos, 1); 
        
        Ashell_print("begin to start experiment");
        // 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;

    }

	return ack;
}
/***********************************************************************************
 * @brief
 * ex:     
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *pres_stop_infpum_callback(user_packet_t *user)
{
	ack_packet_t *ack;
	
	float rate = 0, target = 0;
    uint8_t pos = 0;
	
	if (user->style == CMD_SET) //设置
	{
		// 用户数据处理
		
		//动作重置
		data_server.press_par.last_act_final = data_server.press_par.act_final;
		data_server.press_par.act_final = ACTION_NONE;
		data_server.valve_resetall(&data_server);            
		data_server.valve_localset(&data_server, (uint8_t)VIRTUAL_VALVE_INF, 0);
		data_server.valve_localset(&data_server, (uint8_t)VIRTUAL_VALVE_PUMP, 0);
		dac8830.Write(&dac8830,0,0);
		//切换状态
		taskENTER_CRITICAL();
		msm = MSM_IDLE;
		rsm = RSM_IDLE;
        taskEXIT_CRITICAL();			
		
        //异常停止实验
        data_server.exp_stop_flag = true;
        
		// 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	}

	return ack;

}

static int provalve_stop_infPum(uint8_t argc, char **argv)
{
	if (argc >= 2)
	{
        if (!strcmp(argv[1], "start"))
		{
		
                //动作重置
                data_server.press_par.last_act_final = data_server.press_par.act_final;
                data_server.press_par.act_final = ACTION_NONE;
                data_server.valve_resetall(&data_server);            
                data_server.valve_localset(&data_server, (uint8_t)VIRTUAL_VALVE_INF, 0);
                data_server.valve_localset(&data_server, (uint8_t)VIRTUAL_VALVE_PUMP, 0);
                dac8830.Write(&dac8830,0,0);
                //切换状态
                taskENTER_CRITICAL();
                msm = MSM_IDLE;
                rsm = RSM_IDLE;
                taskEXIT_CRITICAL();			

                //异常停止实验
                data_server.exp_stop_flag = true;

        }
    }
    else
    {
        Ashell_print("Please input provalve_stop_infPum <start> \r\n");
    }
    return 1;
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), stop_infPum, provalve_stop_infPum, stop infPum operation);

/******************* (C) COPYRIGHT 2021 CIQTEK Samuel *****END OF FILE****/

